import {useState} from 'react'
const useWebsocket = (url: string) => {
    let ws = new WebSocket(url)
    const [message, setMessage] = useState('') 
    ws.onmessage = (e) => {
        setMessage(e.data)
    }
    const sendMessage = (msg: string) => {
        ws.send(msg)
    }
    ws.onclose = () => {
        ws = new WebSocket(url)
    }
    return {
        message,
        sendMessage
    }
}

export default useWebsocket